// do /Users/shoude/Dropbox/eegap/EEgap_data_code_heter_SM/Code/Correlate_Fox_BB.do

 
pause on


// Distribution of Misperceptions at the county level
/*
	1. We extract the gap average vs marginal price from BB data at the county level
	2. We extract the distribution of mispercetion for each income group
	3. We compute an average distribution for all income using the income share of each county.
	4. We use the sample to compute income share for each county in our sample
	5. We match this average county-level distribution with BB marginal-average gap at the county 	level.
	6. We regress the distribution 4q

*/


global  pathresults = "/Users/shoude/Dropbox/eegap/EEgap_data_code_heter_SM/Results_estimation"
global  Est_pathresults = "/Users/shoude/Dropbox/eegap/EEgap_data_code_heter_SM/Data_JPEmicro/Matlab_estimation"
global datapath = "/Users/shoude/Dropbox/eegap/EEgap_data_code_heter_SM/Data/"
global BBdatapath = "/Users/shoude/Dropbox/eegap/EEgap_data_code_heter_SM/Data_JPEmicro/ElectricityPrices/"




// Step 1. Extract the gap average vs marginal price from BB data at the county level BB Data: 

use  "$BBdatapath/county_elec_price_social_2007_2012", clear
	gen gap_avc_pmc = avc-pmc
	sort county_utility
	ren county_utility county
pause
	collapse(mean) gap_avc_pmc, by(county)
	sort county
save  "$BBdatapath/gap_avc_pmc_BB_2007_2012", replace

// Step 2. We extract the distribution of mispercetion for each income group
// do /Users/shoude/Dropbox/eegap/EEgap_data_code_heter_SM/Code_JPEmicro/Extract_Fox_theta_4q_avg_bs.do see options


// Step 3. We compute an average distribution for all income using the income share of each county.
use $pathresults/Theta_pdf_4q_Fox_discrete_v44000_inc6_sd7, replace
sort theta_4q

merge theta_4q using $pathresults/Theta_pdf_4q_Fox_discrete_v44000_inc5_sd3
tab _m 
drop _m
sort theta_4q

merge theta_4q using $pathresults/Theta_pdf_4q_Fox_discrete_v44000_inc4_sd4
tab _m 
drop _m
sort theta_4q

merge theta_4q using $pathresults/Theta_pdf_4q_Fox_discrete_v44000_inc3_sd6
tab _m 
drop _m
sort theta_4q

merge theta_4q using $pathresults/Theta_pdf_4q_Fox_discrete_v44000_inc2_sd3
tab _m 
drop _m
sort theta_4q

merge theta_4q using $pathresults/Theta_pdf_4q_Fox_discrete_v44000_inc1_sd10
tab _m 
drop _m

sort theta_4q

save $pathresults/Theta_pdf_4q_Fox_discrete_v44000_all_inc, replace



pause


// Step 4. We use the sample to compute income share for each county in our sample

use "$datapath/Matlab_estimation/share_inc_heter_county", clear
	expand 4
	sort county
	by county: egen theta_4q = seq()
	sort theta_4q
	merge m:1 theta_4q using $pathresults/Theta_pdf_4q_Fox_discrete_v44000_all_inc
	tab _m
	drop _m
	
	gen weight_avg= share_inc_heter1*mean_weight_1 + share_inc_heter2*mean_weight_2 + share_inc_heter3*mean_weight_3 + share_inc_heter4*mean_weight_4 + share_inc_heter5*mean_weight_5 + share_inc_heter6*mean_weight_6

pause	
	
 keep county theta_4q weight_avg
 reshape wide weight_avg, i(county) j(theta_4q)
 
 sort county
save $pathresults/Theta_pdf_4q_Fox_discrete_v44000_all_inc_bycounty, replace
 
 
pause
 
// Step	5. We match this average county-level distribution with BB marginal-average gap at the county 	level.
 
merge county using "$BBdatapath/gap_avc_pmc_BB_2007_2012"
tab _m
keep if _m==3
drop _m
 
save "$BBdatapath/Theta_pdf_4q_Fox_discrete_v44000_all_inc_with_gap_avc_pmc_BB", replace  
 
//Step 6. We regress the distribution 4q
 gen lg_weight_avg1 = log(weight_avg1)
 gen lg_weight_avg2 = log(weight_avg2)
 gen lg_weight_avg3 = log(weight_avg3)
 gen lg_weight_avg4 = log(weight_avg4)
 gen lg_gap_avc_pmc = log(gap_avc_pmc)
 
 
 reg lg_weight_avg1 lg_gap_avc_pmc
 reg lg_weight_avg2 lg_gap_avc_pmc 
 reg lg_weight_avg3 lg_gap_avc_pmc 
 reg lg_weight_avg4 lg_gap_avc_pmc
 
 
cd "/Users/shoude/Dropbox/eegap/EEgap_data_code_heter_SM/Results_FigurePaper_JPEmicro"
 
hist weight_avg1, lcolor(grey) fcolor(navy) graphregion(ifcolor(white) icolor(white) lcolor(white) fcolor(white))  legend( off ) xtitle(Share of Consumers)
graph export "hist_misp_weight_avg1_county.eps", as(eps) preview(off) replace
graph export "hist_misp_weight_avg1_county.png", as(pdf) replace 
 
hist weight_avg2, lcolor(grey) fcolor(navy) graphregion(ifcolor(white) icolor(white) lcolor(white) fcolor(white))  legend( off ) xtitle(Share of Consumers)
graph export "hist_misp_weight_avg2_county.eps", as(eps) preview(off) replace
graph export "hist_misp_weight_avg2_county.png", as(pdf) replace  
 
hist weight_avg3, lcolor(grey) fcolor(navy) graphregion(ifcolor(white) icolor(white) lcolor(white) fcolor(white))  legend( off ) xtitle(Share of Consumers)
graph export "hist_misp_weight_avg3_county.eps", as(eps) preview(off) replace
graph export "hist_misp_weight_avg3_county.png", as(pdf) replace   
 
hist weight_avg4, lcolor(grey) fcolor(navy) graphregion(ifcolor(white) icolor(white) lcolor(white) fcolor(white))  legend( off ) xtitle(Share of Consumers)
graph export "hist_misp_weight_avg4_county.eps", as(eps) preview(off) replace
graph export "hist_misp_weight_avg4_county.png", as(pdf) replace  
